package br.com.uniban.entity;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

@NamedQueries({ @NamedQuery(name = "ProvaResp.feitas", query = "select p from ProvaRespondida p where p.usuario.id = :idUsuario"), 
				@NamedQuery(name = "ProvaResp.criadas", query = "select p from ProvaRespondida p where p.prova.id = :idProva"),
				@NamedQuery(name = "ProvaResp.verProvaResp", query = "select p from ProvaRespondida p join p.prova.listaDeQuestoes q where p.id = :idProva order by q.num_questao asc")
	})
@Entity
@Table(name = "tb_prova_resp")
public class ProvaRespondida implements Serializable {
	@Transient
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = "id_prova_resp")
	private Integer id;
	@OneToOne(optional = true, fetch = FetchType.LAZY)
	@JoinColumn(name = "id_prova", referencedColumnName = "id_prova")
	private Prova prova;

	@OneToOne
	@JoinColumn(name = "id_Usuario", referencedColumnName = "id_Usuario")
	private Usuario usuario;

	@Temporal(TemporalType.DATE)
	private Date publicacao;

	@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
	@JoinTable(name = "tb_prova_resp_join_quest_resp", joinColumns = { @JoinColumn(name = "id_prova_resp", referencedColumnName = "id_prova_resp") }, inverseJoinColumns = { @JoinColumn(name = "id_QuestoesResp", referencedColumnName = "id_prova_quest_resp") })
	private List<ProvaQuestoesRespondidas> listQuestoesRespondida;

	public ProvaRespondida() {
		super();
	}

	public ProvaRespondida(Prova prova, Usuario usuario, Date publicacao,
			List<ProvaQuestoesRespondidas> listQuestoesRespondida) {
		super();
		this.prova = prova;
		this.usuario = usuario;
		this.publicacao = publicacao;
		this.listQuestoesRespondida = listQuestoesRespondida;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Prova getProva() {
		return prova;
	}

	public void setProva(Prova prova) {
		this.prova = prova;
	}

	public Usuario getUsuario() {
		return usuario;
	}

	public void setUsuario(Usuario usuario) {
		this.usuario = usuario;
	}

	public Date getPublicacao() {
		return publicacao;
	}

	public void setPublicacao(Date publicacao) {
		this.publicacao = publicacao;
	}

	public List<ProvaQuestoesRespondidas> getListQuestoesRespondida() {
		return listQuestoesRespondida;
	}

	public void setListQuestoesRespondida(
			List<ProvaQuestoesRespondidas> listQuestoesRespondida) {
		this.listQuestoesRespondida = listQuestoesRespondida;
	}

	/*@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((publicacao == null) ? 0 : publicacao.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime
				* result
				+ ((listQuestoesRespondida == null) ? 0
						: listQuestoesRespondida.hashCode());
		result = prime * result + ((prova == null) ? 0 : prova.hashCode());
		result = prime * result + ((usuario == null) ? 0 : usuario.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ProvaRespondida other = (ProvaRespondida) obj;
		if (publicacao == null) {
			if (other.publicacao != null)
				return false;
		} else if (!publicacao.equals(other.publicacao))
			return false;
		if (id == null) {
			if (other.id != null)
				return false;
		} else if (!id.equals(other.id))
			return false;
		if (listQuestoesRespondida == null) {
			if (other.listQuestoesRespondida != null)
				return false;
		} else if (!listQuestoesRespondida.equals(other.listQuestoesRespondida))
			return false;
		if (prova == null) {
			if (other.prova != null)
				return false;
		} else if (!prova.equals(other.prova))
			return false;
		if (usuario == null) {
			if (other.usuario != null)
				return false;
		} else if (!usuario.equals(other.usuario))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ProvaRespondida [id=" + id + ", prova=" + prova + ", usuario="
				+ usuario + ", publicacao=" + publicacao
				+ ", listQuestoesRespondida=" + listQuestoesRespondida + "]";
	}*/

}
